import http from '@src/util/http'
import { objectDateToTimestamp } from 'pub-bbx-utils'
import GrayUtil from '@src/util/gray'

import { getCustomerDetailResult } from '@model/param/out/Customer';
import { getSystemNormalSearchInputForLength } from '@model/utils/getSystemConfig'
import { disposeProductSearchModelDate } from './ProductApi';
const prefix = '/api/customer/outside';

/** 
 * 获取客户信息
 * @param {string} id - 客户id
 * @returns Promise<Customer>
 */
export function getCustomer(id: string): Promise<getCustomerDetailResult> {
  return http.get('/customer/get', { id })
}

/** 
 * 获取客户信息，用于客户编辑
 * @param {string} id - 客户id
 * @returns Promise<Customer>
 */
export function getForEdit(id: any) {
  return http.get('/customer/getForEdit', {id})
}

/**
 * 获取客户最新的一条可见日志
 * @param {string} customerId - 客户id
 * @returns Promise<Customer>
 */
export function getUpdateRecord(customerId: {} | undefined){
  return http.get('/customer/record/latestOne', customerId)
}

/**
 * 切换客户关注状态
 * @param {object} params - 参数
 * @param {string} params.customerId - 客户id
 * @param {string} params.module - 客户模块： customer
 * @param {string} params.action - 行为, 值为: 关注 或 取消关注
 */ 
export function toggleAttention(params: {} | undefined){
  return http.post('/customer/record/attention/customer', params, false)
}

/** 
 * 查询某一客户的已关注列表
 * @param {object} params - 参数
 * @param {string} params.customerId - 客户id
 * @returns MsgModal<ArrayList<Map>>
 */
export function attentionList(params: {} | undefined){
  return http.get('/customer/record/attention/list', params)
}

/**
 * 取消某些用户对某客户的关注
 * @param {object} params - 参数
 * @param {string} params.customerId - 客户id
 * @param {string} params.userIds - 用户ids, 使用`,`拼接
 */
export function cancelAttention(params: {} | undefined){
  return http.post('/customer/record/attention/delete/customer_ids', params, false);
}
/**
 * 自动匹配团队
 * @param {Object} params - 参数
 * @param {string} params.province - 省
 * @param {string} params.city - 市
 * @param {string} params.dist - 区
 * 
 * @returns MsgModal<List<Team>> 匹配后的数据
 */
export function matchTag(params: {} | undefined){
  return http.get('/customer/tagMatch', params)
}

/**
 * 客户唯一性验证
 * @param {Object} params - 参数
 * @param {string} params.id - 客户id
 * @param {string} params.fieldName - 字段名
 * @param {string} params.value - 值
 * @returns 成功 - {error: message}， 失败 - {ok: ''}
 */
export function unique(params: {} | undefined){
  return http.post('/customer/unique', params, false, {cancelable: false})
}

/**
 * 获取客户提醒
 * @param {Object} params - 参数
 * @param {String} params.modalId - customerId || productId
 * @param {String} params.modal - customer || product
 */
export function getRemindOfCustomer(params: {} | undefined) {
  return http.get('/customer/remind/list', params)
}

/**
 *
 * @param {Object} params - 参数
 * @param {String} params.id - 记录id
 * @returns {*}
 */
export function deleteComment(params: {} | undefined) {
  return http.post('/customer/deleteCustomerRecord', params, false)
}

/**
 *
 * @param {Object} params - 参数
 * @param {String} params.customerId - 客户id
 * @param {Number} params.pageSize -
 * @param {Number} params.pageNum -
 * @returns {*}
 */
export function getLinkmanOfCustomer(params: {} | undefined) {
  return http.get('/customer/linkman/list', params)
}

/**
 *
 * @param {Object} params - 参数
 * @param {String} params.customerId - 客户id
 * @param {Number} params.pageSize -
 * @param {Number} params.pageNum -
 * @param {Number} params.keyword -
 * @returns {*}
 */
export function getUserTag(params: {} | undefined) {
  return http.get('/customer/userTag/list', params)
}

/**
 * 统计给选中的客户发送短信的数量
 * @param {Object} params - 参数
 * @param {String} params.ids - 客户ids
 * @param {Number} params.isAllLm - 是否是全部联系人
 */
export function computeSendNum(params: {} | undefined) {
  return http.post('/customer/computeSendNum', params)
}


/**
 * 统计给选中的客户发送短信的数量
 * @param {Object} params - 参数
 * @param {String} params.ids - 客户ids
 * @param {Number} params.isAllLm - 是否是全部联系人
 */
 export function productComputeSendNum(params: {} | undefined) {
  return http.post('/customer/product/computeSendNum', params)
}

/**
 * 选择客户批量发送短信
 * @param {Object} params - 参数
 * @param {Number} params.smsTemplateId - 短信模板id
 * @param {String} params.ids - 产品ids
 * @param {Number} params.isAllLm - 是否是全部联系人
 * @param {Date} params.sendTime - 发送时间
 */
export function sendSmsBatch(params: {} | undefined) {
  return http.post('/customer/sendSmsBatch', params, false);
}

/**
 * 选择客户批量发送短信
 * @param {Object} params - 参数
 * @param {Number} params.smsTemplateId - 短信模板id
 * @param {String} params.ids - 产品ids
 * @param {Number} params.isAllLm - 是否是全部联系人
 * @param {Date} params.sendTime - 发送时间
 */
 export function productSendSmsBatch(params: {} | undefined) {
  return http.post('/customer/product/sendSmsBatch', params, false);
}

/**
 * 批量编辑客户的某个字段
 * @param {Object} params - 参数
 * @param {String} params params.ids - 客户ids
 * @param {String} params params.mapJson - 修改值
 * @returns {*}
 */
export function batchEditCustomer(params: {} | undefined) {
  return http.post('/customer/editBatch', params, false);
}

/**
 * 获取客户列表
 * @param {Object} params - 参数
 * @returns {*}
 */
export function getCustomerListAsyn(params: {} | undefined) {
  let customerPreFixedPath = GrayUtil.getCustomerApiPath();
  return http.get(`${customerPreFixedPath}/customer/getListAsyn`, params);
}

/**
 * 根据客户id获取客户信息和客户负责人信息和开关
 * @param {Object} params - 参数
 * @param {String} params.id - 客户di
 */
export function getCustomerExeinsyn(params: {} | undefined) {
  return http.get('/customer/exeinsyn/get', params, false);
}

/**
 * 查询客户列表
 * @param {Object} params - 参数
 */
export function getCustomerList(params: any = {}) {
  // 国际化时间改造 增加时间戳参数
  objectDateToTimestamp(params, [
    'createTimeStart',
    'createTimeEnd',
    'updateTimeStart',
    'updateTimeEnd',
  ])
  return http.post('/customer/list', params);
}

/**
 * 查询客户列表
 * @param {Object} params - 参数
 */
export function getCustomerSearchList(params: {} | undefined) {
  return http.post(`/api/elasticsearch/outside/es/customer/searchList`, params);
}

/**
* 客户表单字段列表
* @param {Object} params - 参数
* @param {String} params.isFromSetting - 是否用于设置页，是：true 否：false
*/
export function getCustomerFields(params: {} | undefined) {
  return http.get('/customer/getCustomerFields', params);
}

/**
* 获取钉钉crm客户 instanceId
*/
export function getDtalkInstanceId(cusId: string) {
  return http.get(`/api/middleware/dingtalk/dd/crm/instanceId?cusId=${cusId}`);
}

/**
 * 同步钉钉crm客户敏感信息
 * @param {Object} params - 参数
 * @param {String} params.corpId - 租户id
 * @param {String} params.cusId - 客户id
 * @param {String} params.linkManId - 联系人id
 * @returns {*}
 */
export function syncCustomerCrmData(params: {} | undefined) {
  return http.get('/api/middleware/dingtalk/dd/crm/information', params, false);
}

/**
 * 获取客户详情页右侧组件筛选存储
 * @param params
 * @returns {*}
 */
export function getCardCache() {
  return http.get(`/api/customer/outside/pc/customer/getCardCache`, {})
}
/**
 * 设置客户详情页右侧组件筛选存储
 * @param params
 *  Array
 * @returns {*}
 */
export function setCardCache(params: {} | undefined) {
  return http.post(`/api/customer/outside/pc/customer/setCardCache`, params)
}

/*
 * 设置客户标签
 */
export function setCustomerTag(params: Record<string, any>) {
  return http.post('/api/customer/outside/customer/tag/update', params);
}

/**
 * 批量更新客户标签
 */
export function batchUpdateCustomerTag(params: Record<string, any>) {
  return http.post('/api/customer/outside/customer/tag/batchUpdate', params);
}
/*
 * 获取客户列表
 */
export function CustomerList(params: Record<string, any>) {
  return http.get('/dd/customer/list', params);
}

export function getValue(params: Record<string, any>){
  return http.get('/api/customer/outside/register/customer/value', params);
  
}
// 根据产品二维码查询小程序码
export function queryAppletQrCode(params = {}) {
  return http.get(`/api/customer/outside/code/queryAppletQrCode`, params)
}

// 根据产品二维码查询小程序码
export function createAppletQrCode(params = {}) {
  return http.post(`/api/customer/outside/code/createAppletQrCode`, params)
}

// 单张下载小程序码图片【web中】
export function downloadAppletQrCode(params = {}) {
  return http.get('/product/downloadAppletQrCode', params)
}

// PC端预览-下载小程序码【customer】
export function appletQrcodeDownloadOne(params = {}) {
  return http.post(`/api/customer/outside/code/appletQrcodeDownloadOne`, params,false)
}

/**
 * @description 修改协同人
*/
export function updateSynergies(params: {} | undefined) {
	return http.post(`/api/customer/outside/pc/customer/updateSynergies`, params);
}

// 创建视图
export function createView(params = {}) {
  return http.post(`${prefix}/view/create`, params, true)
}

// 删除视图
export function deleteView(id: any) {
  return http.get(`${prefix}/view/delete/${id}`)
}

// 获取视图列表
export function getViewList(params = {}) {
  return http.get(`${prefix}/view/list`, params)
}

// 更新视图
export function updateView(params = {}) {
  return http.post(`${prefix}/view/update`, params, true)
}

// 修改问券答案
export function updateAnswer(params = {}) {
  return http.post(`${prefix}/satisfactionApply/updateAnswer`, params, true)
}
// 客户权限
export function customerCheckAuth(params = {}){
  return http.get(`${prefix}/pc/customer/checkAuth`, params)
}

// 合同附加组件权限
export function getContractGray (params = {}){
  return http.get(`${prefix}/quality/grayFunction`, params)
}
// 客户和产品列表发送活动
export function sendActivities(params = {}){
  return http.post(`${prefix}/satisfactionApply/sendActivities`, params, true)
}

// 获取活动下拉列表
export function getActivitiesList(params = {}){
  return http.get(`${prefix}/satisfactionApply/getActivitiesList`, params)
}

// 发送邮件
export function sendEmail(params = {}){
  return http.post(`${prefix}/pc/customer/sendEmail`, params, true)
}

/**
 * @description 获取客户地址数据
 * @param {Object} params -- 参数对象
 * @param {String} params.pageSize -- 页码大小
 * @param {String} params.pageNum -- 分页数
 * @param {String} params.keyword -- 关键字
 * @param {String} params.customerId -- 客户id
 */
 export function getTaskCustomerAddress(params: {} | undefined) {
  return http.get('/api/customer/outside/pc/product/address', params);
}

/**
 * @description 获取客户产品数据
 * @param {Object} params -- 参数对象
 * @param {String} params.pageSize -- 页码大小
 * @param {String} params.pageNum -- 分页数
 * @param {String} params.keyword -- 关键字
 * @param {String} params.customerId -- 客户id
 */
 export function getTaskCustomerProduct(params: {} | undefined) {
  // 所有涉及产品的es搜索 关键词超过三十个字自动截取前三十个字
  if((params as any)?.keyword){
		(params as any).keyword = getSystemNormalSearchInputForLength((params as any).keyword)
	}
  disposeProductSearchModelDate(params)
  return http.post('/task/customer/product', params);
}


/**
 * @des 保存客户表单
 */
 export function customerSaveFields(params: {} | undefined) {
  return http.post('/setting/customer/saveFields', params);
}

/**
 * 根据客户id获取客户协同人
 * @param {Object} params - 参数
 * @param {String} params.id - 客户di
 */
export function getSynergiesByCustomer(params: string) {
  return http.get(`${prefix}/pc/customer/getTaskDefault/synergiesByCustomer/${params}`);
}

/**
 * 联系人表单查询
 * @param {Object} params - 参数
 * @param {String} params.id - 客户di
 */
export function getLinkmanFields(params: {} | undefined) {
  return http.get('/api/customer/outside/linkman/template/list');
}

/**
 * 客户开关隐藏
 * @param {Object} params - 参数
 */
export function getTenantConfigByCodeList(params: {} | undefined) {
  return http.post('/api/task/outside/config/getTenantConfigByCodeList', params);
}

/**
 * 获取上级客户列表
 * @param {Object} params - 参数
 */
export function getParentCustomerList(params: {} | undefined) {
  return http.post('/api/elasticsearch/outside/es/customer/searchList', params);
}

/**
 * 获取上级客户树形结构
 * @param {Object} params - 参数
 */
export function getParentCustomerTree(params: {} | undefined) {
  return http.get('/api/elasticsearch/outside/es/customer/tree', params);
}

/**
 * 合并客户
 * @param {Object} params - 参数
 */
export function mergeCustomers(params: {} | undefined) {
  return http.post('/api/customer/outside/pc/customer/mergeCustomers', params);
}